package com.xxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.Dao.BookDao;
import com.xxx.entity.Books;
import com.xxx.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service("BookService")
public class BookServiceImpl extends ServiceImpl<BookDao,Books> implements BookService {

    @Autowired
    BookDao bookDao;

    @Override
    public Books getById(Integer id) {
        Assert.notNull(id,"id不能为空");
        return super.getById(id);
    }

    @Override
    public List<Books> getAll() {
        return super.list();
    }

    @Override
    public List<Books> getByMenuId(Integer menuId) {
        Assert.notNull(menuId,"菜单ID不能为空");

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("menu_id",menuId);

        List list = bookDao.selectList(wrapper);
        return list;
    }

    @Override
    public int saveBook(Books books) {
        if(null == books){
            return 0;
        }

        boolean save = super.save(books);
        return save == true ? 1 : 0;
    }

    @Override
    public int update(Books books) {
        if(null == books || books.getId() == null){
            return 0;
        }

        return super.updateById(books) == true ? 1 : 0;
    }

    @Override
    public int delete(Integer... ids) {
        if(ids.length == 0)
            return 0;
        boolean remove = super.removeByIds(Arrays.asList(ids));
        return remove == true ? ids.length : 0;
    }

    @Override
    public int deleteByMenuId(Integer menuId){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("menu_id",menuId);
        int delete = bookDao.delete(wrapper);
        return delete;
    }
}
